<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>更换头像</span>
    </div>
    <div>
      <!-- 图片，用来展示用户选择的头像 -->
      <img v-if="avatar" :src="avatar" alt=""  class="preview"/>
      <img v-else src="../../../../assets/images/avatar.jpg" alt="" class="preview" />

      <!-- 按钮区域 -->
      <div class="btn-box">
         <input type="file" accept="image/*" style="display: none;" ref="iptRef" @change="filesChange"/>
        <el-button type="primary" icon="el-icon-plus" @click="$refs.iptRef.click()">选择图片</el-button>
        <el-button type="success" icon="el-icon-upload" :disabled='!avatar' @click="uploadAvatar">上传头像</el-button>
      </div>
    </div>
  </el-card>
</template>

<script>

export default {
  name: 'UserAvatar',
  data() {
    return {
      avatar: ''
    }
  },
  methods: {
    filesChange(e) {
      const file = e.target.files[0]
      if (!file) {
        this.avatar = ''
      } else {
        // 1. 创建 FileReader 对象
        const fr = new FileReader()
        // 2. 调用 readAsDataURL 函数，读取文件内容
        fr.readAsDataURL(file)
        // 3. 监听 fr 的 onload 事件
        fr.onload = e => {
          // 4. 通过 e.target.result 获取到读取的结果，值是字符串（base64 格式的字符串）
          this.avatar = e.target.result
        }
      }
    },
    async uploadAvatar() {
      const { data: res } = await this.$http.patch('/my/update/avatar', { avatar: this.avatar })
      if (res.code !== 0) return this.$message.error(res.message)
      this.$message.success(res.message)
      this.avatar = ''
      // 请求成功则更新头像
      this.$store.dispatch('user/updateUserInfo')
    }
  }
}
</script>

<style lang="less" scoped>
.btn-box {
  margin-top: 10px;
}
.preview {
  width: 200px;
  height: 200px;
  object-fit: cover;
}
</style>
